<?php
/**
 * NOTICE OF LICENSE
 *
 * This source file is subject to the  Academic Free License (AFL 3.0)
 * that is available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to support@zprofess.com so we can send you a copy immediately.
 *
 * @category   	Prestashop module
 * @package		Review & Rating Synthesis
 * @version 	1.0.2
 * @author 		Rukan <rukanhitokiri@zprofess.com>
 * @copyright   Copyright (c) 2012 ZPROFESS. (http://www.zprofess.com)
 * @license    	http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */

include_once('AdminZProductReview.php');
include_once('AdminZProductCriterion.php');
include_once('AdminZCustomerBadges.php');

class AdminZProductReviewRating extends AdminTab
{
	/** 
	 * @var object AdminZProductReview instance 
	 */
	private $_adminZProductReview;

	/** 
	 * @var object AdminZProductCriterion instance  
	 */
	private $_adminZProductCriterion;
	
	/** 
	 * @var object AdminZCustomerBadges instance  
	 */
	private $_adminZCustomerBadges;
	
	/** 
	 * @var string review table  
	 */
	private $_review = 'zproduct_review';
	
	/** 
	 * @var string criterion table  
	 */
	private $_criterion = 'zproduct_criterion';
	
	/** 
	 * @var string badges table  
	 */
	private $_badges = 'zcustomer_badges';
	
	public function __construct()
	{
		$this->_adminZProductReview = new AdminZProductReview();
		$this->_adminZProductCriterion = new AdminZProductCriterion();
		$this->_adminZCustomerBadges = new AdminZCustomerBadges();
		$this->table = array($this->_review, $this->_criterion,$this->_badges, 'product', 'customer');
		
		parent::__construct();
		
		$this->_adminZProductReview->token = $this->token;
		$this->_adminZProductCriterion->token = $this->token;
		$this->_adminZCustomerBadges->token = $this->token;
	}
	
	/**
	 * Check rights to view the current tab
	 * @view AdminTab::viewAccess()
	 * 
	 * @return boolean
	 */
	public function viewAccess($disable = false)
	{
		$result = parent::viewAccess($disable);
		$this->_adminZProductReview->tabAccess = $this->tabAccess; 
		$this->_adminZProductCriterion->tabAccess = $this->tabAccess;
		$this->_adminZCustomerBadges->tabAccess = $this->tabAccess;
		return $result;
	}
	
    /**
	 * Manage page processing
	 * @view AdminTab::postProcess()
	 *
	 * @global string $currentIndex Current URL in order to keep current Tab
	 */
	public function postProcess()
	{
		$this->_adminZProductReview->postProcess();
		$this->_adminZProductCriterion->postProcess();
		$this->_adminZCustomerBadges->postProcess();
	}
	
	/**
	 * Display errors
	 * @view AdminTab::displayErrors()
	 */		     
	public function displayErrors()
	{
		parent::displayErrors();
		$this->_adminZProductReview->displayErrors();
		$this->_adminZProductCriterion->displayErrors();
		$this->_adminZCustomerBadges->displayErrors();
	}
	
	/**
	 * Manage page display (form, list...)
	 * @view AdminTab::display()
	 *
	 * @global string $currentIndex Current URL in order to keep current Tab
	 */
	public function display()
	{
		global $currentIndex;

		if (((Tools::isSubmit('submitAdd'.$this->_review) OR Tools::isSubmit('submitAdd'.$this->_review.'AndStay')) AND sizeof($this->_adminZProductReview->_errors)) OR isset($_GET['update'.$this->_review]) OR isset($_GET['add'.$this->_review])) {
			$this->_adminZProductReview->displayForm($this->token);	
			echo '<br /><br /><a href="'.$currentIndex.'&token='.$this->token.'"><img src="../img/admin/arrow2.gif" /> '.$this->l('Back to list').'</a><br />';
		} elseif (((Tools::isSubmit('submitAdd'.$this->_criterion) OR Tools::isSubmit('submitAdd'.$this->_criterion.'AndStay')) AND sizeof($this->_adminZProductCriterion->_errors)) OR isset($_GET['update'.$this->_criterion]) OR isset($_GET['add'.$this->_criterion]) 
		OR (isset($_POST['x']) AND isset($_POST['y'])) ) {
			$this->_adminZProductCriterion->displayForm($this->token);	
			echo '<br /><br /><a href="'.$currentIndex.'&token='.$this->token.'"><img src="../img/admin/arrow2.gif" /> '.$this->l('Back to list').'</a><br />';
		} elseif (((Tools::isSubmit('submitAdd'.$this->_badges) OR Tools::isSubmit('submitAdd'.$this->_badges.'AndStay')) AND sizeof($this->_adminZCustomerBadges->_errors)) OR isset($_GET['update'.$this->_badges]) OR isset($_GET['add'.$this->_badges]) 
		OR (isset($_POST['x']) AND isset($_POST['y'])) ) {
			$this->_adminZCustomerBadges->displayForm($this->token);	
			echo '<br /><br /><a href="'.$currentIndex.'&token='.$this->token.'"><img src="../img/admin/arrow2.gif" /> '.$this->l('Back to list').'</a><br />';
		} else {	
			echo '<h2>'.$this->l('Reviews & Ratings').'</h2>';
			$this->_adminZProductReview->display($this->token);
			echo '<div style="margin:10px">&nbsp;</div>';
			
			echo '<h2>'.$this->l('Badges').'</h2>';
			$this->_adminZCustomerBadges->display($this->token);
			echo '<div style="margin:10px">&nbsp;</div>';
			
			echo '<h2>'.$this->l('Criteria').'</h2>';
			$this->_adminZProductCriterion->display($this->token);
		}
	}
}
